
# Absenteeism outcomes ----------------------------------------------------


# Remove any variables from imputation where too many values are missing:
##Teacher_confronted and students_from_tradingcenter has many missing values, because these questions were only asked to teachers.
cannot_be_imputed <- c("teacher_confronted","students_from_tradingcenter"
)

absenteeism_outcomes_el <- c(
	absenteeism_outcomes_old[!absenteeism_outcomes_old %in% cannot_be_imputed],
	abs_outcomes_moderators_new[!abs_outcomes_moderators_new %in% cannot_be_imputed])

# We can only impute these where we have both ml and el
absenteeism_outcomes_ml <- c(
	paste0(
		absenteeism_outcomes_old[!absenteeism_outcomes_old %in% cannot_be_imputed],
		"_ml"
	)
)

# First, impute variables for compliers all together

temporary_data <- mice(data = el[el$respondent_category == "Complier",c(absenteeism_outcomes_el,absenteeism_outcomes_ml)],m = 1,seed = 1234567)
temporary_data <- mice::complete(temporary_data)

el[el$respondent_category == "Complier",c(absenteeism_outcomes_el,absenteeism_outcomes_ml)] <- temporary_data[,c(absenteeism_outcomes_el,absenteeism_outcomes_ml)]
stopifnot(all(apply(el[el$respondent_category == "Complier",c(absenteeism_outcomes_el,absenteeism_outcomes_ml)],2,function(x)sum(is.na(x))) == 0))

# Now, impute just the el variables for all other respondents

temporary_data <- mice(data = el[,absenteeism_outcomes_el],m = 1,seed = 1234567)
temporary_data <- complete(temporary_data)

el[,absenteeism_outcomes_el] <- temporary_data[,absenteeism_outcomes_el]
stopifnot(all(apply(el[,absenteeism_outcomes_el],2,function(x)sum(is.na(x))) == 0))

rm(temporary_data,cannot_be_imputed)

# Adult covariates --------------------------------------------------------

cannot_be_imputed <- c(
  "minority_religion"
)

covariates_el <- c(
  notin(covariates_old,cannot_be_imputed),notin(covariates_new,cannot_be_imputed)
)

covariates_ml <- c(
  paste0(notin(covariates,cannot_be_imputed),"_ml")
)


temporary_data <- mice(data = el[el$respondent_category == "Complier",c(covariates_el,covariates_ml)],m = 1,seed = 1234567)
temporary_data <- mice::complete(temporary_data)

el[el$respondent_category == "Complier",c(covariates_el,covariates_ml)] <- temporary_data[,c(covariates_el,covariates_ml)]
#stopifnot(all(apply(el[el$respondent_category == "Complier",c(covariates_el,covariates_ml)],2,function(x)sum(is.na(x))) == 0))



temporary_data <- mice(data = el[,covariates_el],m = 1,seed = 1234567)
temporary_data <- mice::complete(temporary_data)

el[,covariates_el] <- temporary_data[,covariates_el]

stopifnot(all(
  apply(el[,covariates_el],2,function(x)sum(is.na(x))) == 0)
  )


# Violence against women outcomes -----------------------------------------

# We can only impute these where we have both ml and el

vaw_outcomes_ml <- c(
	paste0(
		vaw_outcomes,
		"_ml"
	)
)


# First, impute variables for compliers all together

temporary_data <- mice(data = el[el$respondent_category == "Complier",c(vaw_outcomes,vaw_outcomes_ml)],m = 1,seed = 1234567)

temporary_data <- mice::complete(temporary_data)

el[el$respondent_category == "Complier",c(vaw_outcomes,vaw_outcomes_ml)] <- temporary_data[,c(vaw_outcomes,vaw_outcomes_ml)]
stopifnot(all(apply(el[el$respondent_category == "Complier",c(vaw_outcomes,vaw_outcomes_ml)],2,function(x)sum(is.na(x))) == 0))

# Now, impute just the el variables for all other respondents

temporary_data <- mice(data = el[,vaw_outcomes],m = 1,seed = 1234567)
temporary_data <- mice::complete(temporary_data)

el[,vaw_outcomes] <- temporary_data[,vaw_outcomes]
stopifnot(all(apply(el[,vaw_outcomes],2,function(x)sum(is.na(x))) == 0))



# Abortion outcomes -------------------------------------------------------

abo_outcomes_ml <- c(
	paste0(
		abo_outcomes,
		"_ml"
	)
)


temporary_data <- mice(data = el[el$respondent_category == "Complier",c(abo_outcomes,abo_outcomes_ml)],m = 1,seed = 1234567)

temporary_data <- mice::complete(temporary_data)

el[el$respondent_category == "Complier",c(abo_outcomes,abo_outcomes_ml)] <- temporary_data[,c(abo_outcomes,abo_outcomes_ml)]
stopifnot(all(apply(el[el$respondent_category == "Complier",c(abo_outcomes,abo_outcomes_ml)],2,function(x)sum(is.na(x))) == 0))


# Now, impute just the el variables for all other respondents

temporary_data <- mice(data = el[,abo_outcomes],m = 1,seed = 1234567)
temporary_data <- mice::complete(temporary_data)

el[,abo_outcomes] <- temporary_data[,abo_outcomes]
stopifnot(all(apply(el[,abo_outcomes],2,function(x)sum(is.na(x))) == 0))




